 using System;
  using NUnit.Framework;
  
using CUtil;

namespace TestCases.Misc
{



    [TestFixture]
    public class PasswordGeneratorFixture
    {
        private PasswordGenerator defaultPwdGen, customPwdGen;
        private string defaultPassword, customPassword;

        public PasswordGeneratorFixture()
        {
        }

       [SetUp]
        public void Init()
        {
            defaultPwdGen = new PasswordGenerator(false);
            customPwdGen = new PasswordGenerator(false);

            
            customPwdGen.ConsecutiveCharacters = false;
            customPwdGen.RepeatCharacters = false;

            defaultPassword = null;
            customPassword = null;
        }

        [TearDown]
        public void Destroy()
        {
            defaultPwdGen = null;
            customPwdGen = null;
            defaultPassword = null;
            customPassword = null;
        }

        [Test]
        public void Default()
        {
            defaultPassword = defaultPwdGen.Generate();

            Assert.AreEqual(6, defaultPwdGen.Minimum, "Minimum should be set to default value.");
            Assert.AreEqual(10, defaultPwdGen.Maximum, "Maximum should be set to default value.");
            Assert.AreEqual(false, defaultPwdGen.ConsecutiveCharacters, "Consecutive characters are not allowed by default.");
            Assert.AreEqual(true, defaultPwdGen.RepeatCharacters, "Repeating characters are  allowed by default.");
            Assert.IsTrue(String.Empty != defaultPassword);
            
        }

    

     
        [Test]
        public void ConsecutiveCharacterConstraints()
        {
            customPassword = customPwdGen.Generate();

            bool hasConsecutive = false;
            for (int i = 0; i < customPassword.Length - 1; i++)
            {
                if (customPassword[i] == customPassword[i + 1])
                {
                    hasConsecutive = true;
                }
            }
            Assert.IsTrue(!(customPwdGen.ConsecutiveCharacters && hasConsecutive));
            
        }

        [Test]
        public void RepeatingCharacterConstraints()
        {
            customPassword = customPwdGen.Generate();

            bool hasRepeating = false;
            Assert.IsFalse(customPassword == string.Empty);

            for (int i = 0; i < customPassword.Length - 1; i++)
            {
                if (-1 != customPassword.IndexOf(customPassword[i], i + 1))
                {
                    hasRepeating = true;
                }
            }
            Assert.IsTrue(!(customPwdGen.RepeatCharacters && hasRepeating));
        }

        [Test]
        public void ExcludeSymbols()
        {
            customPwdGen.ExcludeSymbols = true;
            customPassword = customPwdGen.Generate();
            Assert.IsFalse(customPassword == string.Empty);
            foreach (char item in customPassword)
            {
                Assert.IsTrue(Char.IsLetterOrDigit(item), "No symbols should be in the resulting password." );
            }
        }

        [Test]
        public void ExcludeCharacters()
        {
            string exclude = "0123456789";
            customPwdGen.Exclusions = exclude;
            customPassword = customPwdGen.Generate();
            foreach (char item in exclude)
            {
                Assert.AreEqual(-1, customPassword.IndexOf(item), "No character in the exclusion set should be in the resulting password.");
            }
        }

       
    }
    
}
